package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.TimeTool;
import com.cskaoyan.bean.User;
import com.cskaoyan.bean.vo.LoginInfoBean;
import com.cskaoyan.bean.vo.LoginInfo;
import com.cskaoyan.service.wx.WxAuthService;
import com.cskaoyan.shiro.MallToken;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author 杨杰
 * @version V1.0
 * @Package com.cskaoyan.controller.wx
 * @date 2020/12/8 10:53
 * @Copyright © 暂时还没有
 */
@RequestMapping("wx/auth")
@Controller
public class WxAuthController {

    @Autowired
    WxAuthService wxAuthService;

    @RequestMapping("login")
    @ResponseBody
    public BaseRespVo login(@RequestBody User user, HttpServletRequest request){

        /*
        验证查询admin表，无账户的自行添加，原账户密码为加密字符
         */
        String username = user.getUsername();
        String password = user.getPassword();

        Subject subject = SecurityUtils.getSubject();
        try {
            MallToken mallToken = new MallToken(username, password, "user");
            //权限认证
            subject.login(mallToken);
        } catch (AuthenticationException e) {
            return BaseRespVo.fail("用户名或密码不正确");
        }

        boolean authenticated=subject.isAuthenticated();
        LoginInfo loginInfo = null;
        LoginInfoBean loginInfoBean = new LoginInfoBean();
        if (authenticated) {//验证通过

            HttpSession session = request.getSession();
            //给session添加name属性，用于取到管理员账号名称
            session.setAttribute("name",username);
            int id = wxAuthService.selectById(username);
            session.setAttribute("id", id);
            loginInfo = wxAuthService.login(user);
            //认证通过，封装响应体
            loginInfoBean.setTokenExpire(TimeTool.getCurrentTime());//调用工具类获取当前时间
            loginInfoBean.setToken(session.getId());//将sessionId传给token
            //加一层封装

            loginInfoBean.setUserInfo(loginInfo);
            return BaseRespVo.ok(loginInfoBean);
        }

        return BaseRespVo.fail("密码错误，请使用test1,test2,test3");

    }
    @RequestMapping("logout")
    public BaseRespVo logout(@RequestBody Object object, HttpServletRequest request, HttpServletResponse response){

        try {
            //转发到首页url
            response.sendRedirect("/wx/home/index");
            Subject subject = SecurityUtils.getSubject();
            subject.logout();//删除权限，session信息
        } catch (IOException e) {
            e.printStackTrace();
        }

        return BaseRespVo.ok("已退出");
    }



}
